package com.googlecode.kipler.container.dl.operational;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.googlecode.kipler.container.dl.ABox;
import com.googlecode.kipler.container.dl.Individual;
import com.googlecode.kipler.container.dl.SHOIQABox;
import com.googlecode.kipler.syntax.Role;
import com.googlecode.kipler.syntax.concept.Concept;
import com.googlecode.kipler.syntax.formula.ObjectRoleAssertion;


/**
 * 
 * @author İnanç Seylan
 *
 */
public class TypedBlockablePredecessorFilter implements
		RelatedIndividualWithTypeFilter {

	private ABox aBox;

	private Role role;

	private Individual owner;

	private Concept concept;

	public ABox getABox() {
		return aBox;
	}

	public void setABox(ABox box) {
		aBox = box;
	}

	public Concept getConcept() {
		return concept;
	}

	public void setConcept(Concept concept) {
		this.concept = concept;
	}

	public Individual getIndividual() {
		return owner;
	}

	public void setIndividual(Individual owner) {
		this.owner = owner;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Set<Individual> filter() {
		SHOIQABox shoiqABox = (SHOIQABox) getABox();
		Set<Individual> result = new HashSet<Individual>();
		Iterator<ObjectRoleAssertion> raIter = shoiqABox.predecessorIterator(
				getRole(), getIndividual());
		while (raIter.hasNext()) {
			ObjectRoleAssertion ra = raIter.next().normalize(
					getIndividual().getName());
			Individual filler = shoiqABox.getIndividual(ra.getFiller());
			if (shoiqABox.isBlockable(filler) && filler.contains(getConcept())) {
				result.add(filler);
			}
		}
		return result;
	}

	public boolean contains() {
		SHOIQABox shoiqABox = (SHOIQABox) getABox();
		boolean found = false;
		Iterator<ObjectRoleAssertion> raIter = shoiqABox.predecessorIterator(
				getRole(), getIndividual());
		while (raIter.hasNext() && !found) {
			ObjectRoleAssertion ra = raIter.next().normalize(
					getIndividual().getName());
			Individual filler = shoiqABox.getIndividual(ra.getFiller());
			if (shoiqABox.isBlockable(filler) && filler.contains(getConcept())) {
				found = true;
			}
		}
		return found;
	}

}
